/**
 * 商品分类，列表，详情
 * @authors yxz (1085388169@qq.com)
 * @date    2015-05-25 15:08:58
 * @version 1.0
 */
(function() {

    var $modalCataEdit = $('#modalEditCata');
    var $modalCataAdd = $('#modalAddCata');
    var $modalCataDel = $('#modalDelCata');

    admin.product = {
        // 显示分类编辑弹窗
        showCataEdit: function() {
            $modalCataEdit.on('show.bs.modal', function(event) {
                var $button = $(event.relatedTarget);
                var $smallBox = $(event.relatedTarget).closest('.small-box');
                // cata info get
                var cataId = $button.parent().data('id');
                var cataName = $smallBox.find('h3').text();
                var isOffline = $smallBox.hasClass('offline');
                //modal info set
                var $cataName = $(this).find('.cata-name');
                var $cataOnoff = $(this).find('.cata-onoff');
                $cataName.val(cataName);
                if (isOffline === true) {
                    $cataOnoff.eq(1).prop('checked',true);
                }else{
                    $cataOnoff.eq(0).prop('checked',true);
                }

                // 提交修改
                $modalCataEdit.on('click', '.btn-submit', function(event) {
                    event.preventDefault();
                    //ajax
                    //....
                    //ajax成功后
                    var title = $cataName.val();
                    var isOffline = $cataOnoff.eq(1).prop('checked');
                    // 修改分类的名称
                    $smallBox.find('h3').text(title);
                    // 修改分类的状态
                    if (isOffline) {
                        $smallBox.addClass('offline');
                    }else {
                        $smallBox.removeClass('offline');
                    }
                    
                    $modalCataEdit.modal('hide');
                });
            });
        },
        // 显示分类增加弹窗
        showCataAdd: function() {
            $modalCataAdd.on('show.bs.modal', function(event) {
                var $cataName = $(this).find('.cata-name');
                var $cataOnoff = $(this).find('.cata-onoff');
                //提交
                $modalCataAdd.on('click', '.btn-submit', function(event) {
                    event.preventDefault();
                    //ajax
                    //....
                    //ajax成功后||获取id
                    var cataName = $cataName.val();
                    var cataItemNum = 0;
                    var cataId = 999;
                    var cataOnoff = "";
                    if ($cataOnoff.eq(1).prop('checked') === true) {
                        cataOnoff = 'offline';
                    }
                    
                    var html = '<!-- ./col -->'+
                                '<div class="col-lg-3 col-xs-6">'+
                                '    <!-- small box -->'+
                                '    <div class="small-box cata-box '+ cataOnoff +' ">'+
                                '        <div class="inner">'+
                                '            <h3>'+ cataName+'</h3>'+
                                '            <p>'+ cataItemNum+'种</p>'+
                                '        </div>'+
                                '        <div class="control" data-id="'+ cataId +'">'+
                                '            <i class="iconfont icon-editor" data-toggle="modal" data-target="#modalEditCata"></i>'+
                                '            <i class="iconfont icon-delete" data-toggle="modal" data-target="#modalDelCata"></i>'+
                                '        </div>'+
                                '        <a href="#" class="small-box-footer">查看详情<i class="fa fa-arrow-circle-right"></i> </a>'+
                                '    </div>'+
                                '</div>';
                    $('.small-box-add').parent().before(html);
                    $modalCataAdd.modal('hide');
                });
            });
            
        },

        // 显示分类删除弹窗
        showCataDel: function() {
            $modalCataDel.on('show.bs.modal', function(event) {
                 // cata info get
            	var $button = $(event.relatedTarget);
                var $smallBox = $(event.relatedTarget).closest('.small-box');
                var cataName = $smallBox.find('h3').text();
                var cataId = $button.parent().data('id');
                console.log(cataId);
                //modal info set
                var $cataName = $(this).find('.modal-body b');
                var $cataId = $(this).find('.modal-body input:hidden');
                $cataName.text(cataName);
                $cataId.val(cataId);

                // 提交
                $modalCataDel.on('click', '.btn-submit', function(event) {
                    event.preventDefault();
                    //ajax
                    //....
                    //ajax成功后
                    //success
                    $smallBox.parent().remove();
                    $modalCataDel.modal('hide');
                });

            });
        },

        /***************************
         *
         *      商品编辑页面
         * 
         ***************************/
        // 提交 基本信息（form post）
        // 获取 基本信息 （php）
        
        // 提交 图片信息 ()
        // 获取 图片信息  

        // 提交 详情信息 ()
        submitDetail:function(editor) {
            editor = editor || um;
            var content = "";
            
            console.log(content);

            // 判断内容长度
            // editor.getContentLength();

            // 提交
            if (editor.hasContents()) {
                console.log('有内容');
                content = editor.getPlainTxt(); 
                //ajax提交

            }else{
                console.log('无内容');
                //提示相关js
            }

        },
        // 获取 详细信息 ()

        // 初始化
        init: function() {
            this.showCataDel();
            this.showCataAdd();
            this.showCataEdit();
        }
    };

})();
